package com.bop.dehong.center;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.center.adapter.TransactionRecordsAdapter;
import com.bop.dehong.http.model.GetTradingRecordModel;
import com.bop.dehong.info.TransactionRecordsInfo;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * 交易记录
 * @author Tangwenhan
 *2015-11-17下午2:28:22
 */
public class TransactionRecordsActivity extends BaseActivity implements OnHeaderRefreshListener,OnFooterRefreshListener{
	
	ListView mListView;
	TransactionRecordsAdapter mAdapter;
	PullToRefreshView mRefreshView;
	
	GetTradingRecordModel mGetRecordModel;
	int mCurrentPage = 1;
	int mTotalPage = 1;
	private int REQ_TYPE = 0;
	private final int REQ_REFRESH = 0;
	private final int REQ_LOAD = 1;
	
	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.jiaoyijilu);
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		mListView = getView(R.id.listView1);
		mRefreshView = getView(R.id.pullToRefreshView1);
		mRefreshView.setOnHeaderRefreshListener(this);
		mRefreshView.setOnFooterRefreshListener(this);
	}

	@Override
	protected void initData() {
		
		mAdapter = new TransactionRecordsAdapter(this);
		mListView.setAdapter(mAdapter);
		getRecord();
	}
	private void getRecord(){
		if (mGetRecordModel == null) {
			mGetRecordModel = new GetTradingRecordModel(this, getCallback);
		}
		showProgressDialog();
		mGetRecordModel.doNet(DeHongApp.mLoginEntity.uid, mCurrentPage+"", "20");
	}
	RequestCallBack<TransactionRecordsInfo> getCallback = new RequestCallBack<TransactionRecordsInfo>() {

		@Override
		public void end() {
			
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(TransactionRecordsInfo info) {
			super.onSuccess(info);
			if (REQ_TYPE == REQ_REFRESH) {
				mAdapter.clear();
			}
			if (info.lists.size() > 0) {
				mAdapter.appendToList(info.lists);
			}else {
				ToastUtils.show(TransactionRecordsActivity.this, "暂无数据！");
			}
			mTotalPage = info.totalpages;
		}
	};
 	
	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.act_transaction_records;
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		mRefreshView.onRefreshComplete();
		REQ_TYPE = REQ_LOAD;
		if (mCurrentPage < mTotalPage) {
			mCurrentPage ++;
			getRecord();
		}else{
			mRefreshView.onRefreshComplete();
			ToastUtils.show(this, "没有数据可加载！");
		}
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		mRefreshView.onRefreshComplete();
		REQ_TYPE = REQ_REFRESH;
		mCurrentPage = 1;
		getRecord();
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

}

